<template>
  <Form class="xtx-form" ref="formCom"  :validation-schema="schema" v-slot="{errors}">
   <h2>{{errors}}</h2>
    <div class="user-info">
      <img :src="avatar" alt="" />
      <p>Hi，{{nickname}} 欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~</p>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <Field name='mobile' v-model="form.mobile" class="input" type="text" placeholder="绑定的手机号" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <Field name='code' v-model="form.code" class="input" type="text" placeholder="短信验证码" />
         <span class="code" @click="sendCodeBtn">{{number? number  : "发送验证码"}}</span>
      </div>
      <div class="error"></div>
    </div>
    <a href="javascript:;" class="submit" @click="loginBtn">立即绑定</a>
  </Form>
</template>

<script>
import { Form, Field } from 'vee-validate'
import schema from '@/utils/vee-validate-schema'
import { ref, reactive, watch } from 'vue'
import { useIntervalFn } from '@vueuse/shared'
import { userQQBindCode, userQQBindLogin } from '@/api/user'
import { useStore } from 'vuex'
import Message from '@/components/library/Message'
import { useRouter } from 'vue-router'
export default {
  name: 'CallbackBind',
  components: {
    Form, Field
  },
  props: {
    unionId: {
      type: String,
      default: ''
    }
  },
  setup (props) {
    const store = useStore()
    const nickname = ref('')
    const avatar = ref('')
    const formCom = ref(null)
    const number = ref(0)
    const router = useRouter()
    if (QC.Login.check()) {
      QC.api('get_user_info').success(({ data }) => {
        nickname.value = data.nickname
        avatar.value = data.figureurl_2
      })
    }
    // 表单数据对象
    const form = reactive({
      mobile: null,
      code: null,
      unionId: ''
    })
    const { resume, pause } = useIntervalFn(
      () => {
        number.value--
        if (number.value <= 0) {
          pause()
        }
      }, 1000, { immediate: false })
    const sendCodeBtn = async () => {
      if (number.value > 0) {
        return
      }
      number.value = 60
      resume()
      try {
        await userQQBindCode(form.mobile)
        Message({ type: 'success', message: '验证码发送成功' })
      } catch (error) {
        Message({ type: 'error', message: error.response.data.message || '获取验证码出现错误' })
      }
    }
    watch(() => props.unionId, () => {
      form.unionId = props.unionId
    }, { immediate: true })
    const loginBtn = async () => {
      const valid = await formCom.value.validate()
      if (!valid) return Message({ type: 'error', message: '表单验证失败' })
      try {
        const { result } = await userQQBindLogin(form)
        console.log(result)
        store.commit('user/setUser', result)
        const redirectUrl = router.currentRoute.value.query.redirectUrl
        router.push(redirectUrl || '/')
      } catch (err) {
        Message({ type: 'error', message: err.response.form.message })
      }
    }
    return { nickname, avatar, formCom, form, number, schema, sendCodeBtn, loginBtn }
  }
}
</script>

<style scoped lang='less'>
.user-info {
    width: 320px;
    height: 70px;
    margin: 0 auto;
    display: flex;
    background: #f2f2f2;
    align-items: center;
    padding: 0 10px;
    margin-bottom: 25px;
    img {
      background: #f2f2f2;
      width: 50px;
      height: 50px;
    }
    p {
      padding-left: 10px;
    }
  }
  .code {
    position: absolute;
    right: 0;
    top: 0;
    line-height: 50px;
    width: 80px;
    color: #999;
    &:hover {
      cursor: pointer;
    }
  }
</style>
